package im.ok.back.controller;

import im.ok.back.common.FileSuffixEnum;
import im.ok.back.common.ServerResponse;
import im.ok.back.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("image")
public class ImageController {

    @PostMapping("gif2jpg")
    public ServerResponse gif2jpg(@RequestParam("file") MultipartFile file) {
        try {
            Map<String, Object> resMap = CommonUtil.saveFile(file, FileSuffixEnum.GIF.getDesc(), FileSuffixEnum.JPG.getDesc());
            return CommonUtil.commonProcess4ImageConvert(resMap, 0);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("getGif2Jpg")
    public void getGif2jpg (String fileName, HttpServletResponse response) {
        CommonUtil.exportFile(fileName, FileSuffixEnum.JPG.getDesc(), response);
    }

    @PostMapping("gif2png")
    public ServerResponse gif2png(@RequestParam("file") MultipartFile file) {
        try {
            Map<String, Object> resMap = CommonUtil.saveFile(file, FileSuffixEnum.GIF.getDesc(), FileSuffixEnum.PNG.getDesc());
            return CommonUtil.commonProcess4ImageConvert(resMap, 1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("getGif2Png")
    public void getGif2png (String fileName, HttpServletResponse response) {
        CommonUtil.exportFile(fileName, FileSuffixEnum.PNG.getDesc(), response);
    }

}
